package com.wd.tw.service;

import com.wd.tw.domain.dto.EleRunDataQuery;
import com.wd.tw.domain.vo.*;
import com.wd.tw.mapper.ElevatorReportMapper;
import org.apache.poi.util.LittleEndian;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @className: ElevatorReportService
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2025/1/10
 **/
@Service
public class ElevatorReportService {

    @Resource
    ElevatorReportMapper reportMapper;


    public List<EleRunTotal> getEleRunTotal(EleRunDataQuery dataQuery) {
        List<EleRunTotal> eleRunTotalList = reportMapper.getEleRunTotal(dataQuery);
        eleRunTotalList.forEach(eleRunTotal -> eleRunTotal.calRunTime());
        List<EleRunTotal> eleRunTotals = groupAndSum(eleRunTotalList);
        return eleRunTotals;

    }


    public static List<EleRunTotal> groupAndSum(List<EleRunTotal> inputData) {
        // 使用Map进行分组，key为(elevatorId, elevatorName)，value为累加后的结果
        Map<String, EleRunTotal> groupedData = inputData.stream().collect(Collectors.toMap(
                data -> data.getElevatorId() + "-" + data.getElevatorName(),
                data -> data,
                (existing, replacement) -> {
                    // 如果存在相同的key（elevatorId, elevatorName），则累加 runTimes 和 startTime
                    existing.setRunTimes(existing.getRunTimes() + replacement.getRunTimes());
                    existing.setStartTime(existing.getStartTime() + replacement.getStartTime());
                    return existing;
                }
        ));

        // 返回分组并累加后的结果
        return new ArrayList<>(groupedData.values());
    }


    public EChartsLineData getRunData(EleRunDataQuery dataQuery) {

        List<EleRunData> runData = reportMapper.getRunData(dataQuery);

        runData.sort(Comparator.comparing(EleRunData::getRecordDate));

        List<String> elevatorList = runData.stream()
                .map(EleRunData::getElevatorName)  // 提取 elevatorId 字段
                .distinct()                      // 去重
                .sorted()                        // 排序
                .collect(Collectors.toList());   // 收集结果到 List<Long> 中


        List<String> dateList = runData.stream()
                .map(EleRunData::getRecordDate)  // 提取 elevatorId 字段
                .distinct()                      // 去重
                .sorted()                        // 排序
                .collect(Collectors.toList());   // 收集结果到 List<Long> 中

        EChartsLineData eChartsLineData = new EChartsLineData();

        eChartsLineData.setXAxisData(dateList);
        List<EChartsDataSeries> dataSeries = new ArrayList<>();

        for (int i = 0; i < elevatorList.size(); i++) {
            String elevatorName = elevatorList.get(i);
            EChartsDataSeries<Long> longEChartsDataSeries = new EChartsDataSeries<>();
            longEChartsDataSeries.setType("line");
            longEChartsDataSeries.setName(elevatorName);
            List<Long> data = runData.stream().filter(item -> item.getElevatorName().equals(elevatorName)).map(EleRunData::getRunTimes).collect(Collectors.toList());
            longEChartsDataSeries.setData(data);
            dataSeries.add(longEChartsDataSeries);
        }
        eChartsLineData.setLegend(elevatorList);

        eChartsLineData.setSeries(dataSeries);

        return eChartsLineData;
    }


    public EChartsLineData getRunTimes(EleRunDataQuery dataQuery) {

        List<EleRunData> runData = reportMapper.getRunTimes(dataQuery);
        runData.sort(Comparator.comparing(EleRunData::getRecordDate));

        List<String> elevatorList = runData.stream()
                .map(EleRunData::getElevatorName)  // 提取 elevatorId 字段
                .distinct()                      // 去重
                .sorted()                        // 排序
                .collect(Collectors.toList());   // 收集结果到 List<Long> 中


        List<String> dateList = runData.stream()
                .map(EleRunData::getRecordDate)  // 提取 elevatorId 字段
                .distinct()                      // 去重
                .sorted()                        // 排序
                .collect(Collectors.toList());   // 收集结果到 List<Long> 中

        EChartsLineData eChartsLineData = new EChartsLineData();

        eChartsLineData.setXAxisData(dateList);
        List<EChartsDataSeries> dataSeries = new ArrayList<>();

        for (int i = 0; i < elevatorList.size(); i++) {
            String elevatorName = elevatorList.get(i);
            EChartsDataSeries<Long> longEChartsDataSeries = new EChartsDataSeries<>();
            longEChartsDataSeries.setType("bar");
            longEChartsDataSeries.setName(elevatorName);
            List<Long> data = runData.stream().filter(item -> item.getElevatorName().equals(elevatorName)).map(EleRunData::getRunTM).collect(Collectors.toList());
            longEChartsDataSeries.setData(data);
            dataSeries.add(longEChartsDataSeries);
        }
        eChartsLineData.setLegend(elevatorList);

        eChartsLineData.setSeries(dataSeries);

        return eChartsLineData;
    }


    public List<EChartsPieData> getPersonCount(EleRunDataQuery dataQuery) {
        List<EleRunPersonCount> runPersonCount = reportMapper.getRunPersonCount(dataQuery);
        List<EChartsPieData> dataList=new ArrayList<>();
        runPersonCount.forEach(item->{
            EChartsPieData eChartsPieData = new EChartsPieData();
            eChartsPieData.setName(item.getElevatorName());
            eChartsPieData.setValue(item.getPersonCount());
            dataList.add(eChartsPieData);
        });
        return dataList;
    }

}
